package com.fann;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LessonOpenControllerServlet
 */
public class LessonOpenControllerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    private LessonOpenDAO dao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LessonOpenControllerServlet() {
        super();
        dao = new LessonOpenDAO();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		String acttype = request.getParameter("acttype");
		
		if (acttype.equals("lessonOpenList")){
			List<LessonOpen> LOs = dao.getAll();
            request.setAttribute("LOs", LOs); 
            
            
            session.setAttribute("left", "/LessonOpen/lessonOpenTree.jsp");
			session.setAttribute("center", "/LessonOpen/lessonOpenList.jsp");
			
		} else if (acttype.equals("lessonOpenAdd")){
			session.setAttribute("center", "/LessonOpen/lessonOpenEdit.jsp");

		} else if (acttype.equals("lessonOpenEdit")){
			LessonOpen LO = dao.getLessonOpenById(request.getParameter("id"));
			session.setAttribute("LO", LO);
			
			session.setAttribute("center", "/LessonOpen/lessonOpenEdit.jsp");
			
		} else if (acttype.equals("lessonOpenDelete")){
			String id = request.getParameter("id");
			int rowCount =0;
			if (id !=null){
				rowCount = dao.delete(id);
			}
			
			session.setAttribute("center", "/LessonOpen/lessonOpenDelete.jsp?rowCount="+ rowCount);
		} 

		request.getRequestDispatcher("default.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		String acttype = request.getParameter("acttype");
		
		String lo_id, lo_name, lo_lecturer, lo_description, lo_objectives, lo_outline, lo_textbooks, lo_Sdate, lo_Edate;
		int id, lo_hours, lo_cost;
		
		if (acttype.equals("lessonOpenAdd")){
			if (request.getParameter("lo_id") != null){
				
						   id =0;
					    lo_id = request.getParameter("lo_id");
					  lo_name = request.getParameter("lo_name");
				  lo_lecturer = request.getParameter("lo_lecturer");
				lo_description= request.getParameter("lo_description");
				lo_objectives = request.getParameter("lo_objectives");

					lo_outline= request.getParameter("lo_outline");
					lo_hours = Integer.parseInt(request.getParameter("lo_hours"));
				lo_textbooks = request.getParameter("lo_textbooks");
					lo_Sdate = request.getParameter("lo_Sdate");
					lo_Edate = request.getParameter("lo_Edate");
					lo_cost  = Integer.parseInt(request.getParameter("lo_cost"));
				
				//Construction
				LessonOpen LO = new LessonOpen(id, lo_id, lo_name, lo_lecturer, lo_description, lo_objectives, lo_outline, lo_hours, lo_textbooks, lo_Sdate, lo_Edate, lo_cost);
				
				//add
				dao.add(LO);

				//Success, Fail
				response.sendRedirect("LessonOpenController?acttype=lessonOpenList");
			} else {
				response.sendRedirect("LessonOpenController?acttype=lessonOpenAdd");
			}
		}

		if (acttype.equals("lessonOpenEdit")){
			
					   id = Integer.parseInt(request.getParameter("id"));
				    lo_id = request.getParameter("lo_id");
				  lo_name = request.getParameter("lo_name");
			  lo_lecturer = request.getParameter("lo_lecturer");
			lo_description= request.getParameter("lo_description");
			lo_objectives = request.getParameter("lo_objectives");
		
				lo_outline= request.getParameter("lo_outline");
				lo_hours = Integer.parseInt(request.getParameter("lo_hours"));
			lo_textbooks = request.getParameter("lo_textbooks");
				lo_Sdate = request.getParameter("lo_Sdate");
				lo_Edate = request.getParameter("lo_Edate");
				lo_cost  = Integer.parseInt(request.getParameter("lo_cost"));
			
			//Construction
				LessonOpen LO = new LessonOpen(id, lo_id, lo_name, lo_lecturer, lo_description, lo_objectives, lo_outline, lo_hours, lo_textbooks, lo_Sdate, lo_Edate, lo_cost);
				
			//update
		    dao.update(LO);
		    
			//Set List
			List<LessonOpen> LOs = dao.getAll();
            request.setAttribute("LOs", LOs);
            
		    response.sendRedirect("LessonOpenController?acttype=lessonOpenList");
		}
	}

}
